#include "Signal.hpp"
#include "type.hpp"
#include "BaseLog.hpp"
#include <csignal>
#include <signal.h>
#include <unordered_map>

static void sig_reg(signal_t sig, void(*sig_cb)(int)) {

    switch (sig) {
    case SIGINT:
    {
        info("SIGINT(%d) register!\n", sig);
        break;
    }
    case SIGHUP:
    {
        info("SIGHUP(%d) register!\n", sig);
        break;
    }
    case SIGQUIT:
    {
        info("SIGQUIT(%d) register!\n", sig);
        break;
    }
    case SIGILL:
    {
        info("SIGILL(%d) register!\n", sig);
        break;
    }
    case SIGABRT:
    {
        info("SIGABRT(%d) register!\n", sig);
        break;
    }
    case SIGFPE:
    {
        info("SIGFPE(%d) register!\n", sig);
        break;
    }
    case SIGKILL:
    {
        info("SIGKILL(%d) register!\n", sig);
        break;
    }
    case SIGSEGV:
    {
        info("SIGSEGV(%d) register!\n", sig);
        break;
    }
    case SIGPIPE:
    {
        info("SIGPIPE(%d) register!\n", sig);
        break;
    }
    case SIGALRM:
    {
        info("SIGALRM(%d) register!\n", sig);
        break;
    }
    case SIGTERM:
    {
        info("SIGTERM(%d) register!\n", sig);
        break;
    }
    case SIGSTOP:
    {
        info("SIGSTOP(%d) register!\n", sig);
        break;
    }
    case SIGCONT:
    {
        info("SIGCONT(%d) register!\n", sig);
        break;
    }
    case SIGCHLD:
    {
        info("SIGICHLD(%d) register!\n", sig);
        break;
    }
    case SIGUSR1:
    {
        info("SIGUSR1(%d) register!\n", sig);
        break;
    }
    case SIGUSR2:
    {
        info("SIGUSR2(%d) register!\n", sig);
        break;
    }
    default: break;
    }

    signal(sig, sig_cb);
}

using namespace DBServer::Base;

void Signaler::init(std::unordered_map<signal_t, void(*)(int)>& signals) {

    for (auto& entry : signals) 
    {
        sig_reg(entry.first, entry.second);
    }
}